//*****************************************************************************	
//*	util.h
//*****************************************************************************	

#ifndef UTIL_H
	#define UTIL_H

	// Macro definitions
	// sbi and cbi are not longer supported by the avr-libc
	// to avoid version-conflicts the macro-names have been 
	// changed to SetBit and ClearBit.
	#define SetBit(sfr, bit) ((sfr) |= (1 << (bit)))
	#define ClearBit(sfr, bit) ((sfr) &= ~(1 << (bit)))

	#define FALSE   0
	#define TRUE    (!FALSE)
	#define NULL    0

	// PROTOTYPES 
	void Initialization(void);
	void showADCLeftRight(void);
	void showADCLineCenter(void);
	void disable_JTAG(void);
	void OSCCAL_calibration(void);
	void Delay(unsigned int millisec);
	void InitTimer(void);
	unsigned long TimerMsCur(void);
	char TimerCheck(unsigned long msStart, unsigned int msWait);
	void TimerWait(unsigned int ms);
	void WaitMs(unsigned int ms);
	void InitPWM(void);
	void SetLeftMotorPWM(int pwm);	
	void SetRightMotorPWM(int pwm);
	void SetupLineSensors(void);
	void InitADC(void);
	
	unsigned char GetADC(char adc);
	int GetADC10(char ad_mux);

	// Compass Defines and Prototypes:
	unsigned char OriginalHeading;
	void CompassDiff(void);
	void ThermNoise(void);
	void CompNoise(void);
	void CompassTest(void);
	void RTurn90(void);
	void LTurn90(void);

	// TerminatingConditions Defines & Prototypes:
	#define	UNTIL_KEYPRESS				1
	#define	UNTIL_RIGHTSENSOR_GREATER	2
	#define	UNTIL_MIDDLESENSOR_GREATER	3
	#define	UNTIL_LEFTSENSOR_GREATER	4
	#define	UNTIL_LINESENSOR_GREATER	5
	#define	UNTIL_RIGHTSENSOR_LOWER		6
	#define	UNTIL_MIDDLESENSOR_LOWER	7
	#define	UNTIL_LEFTSENSOR_LOWER		8
	#define	UNTIL_LINESENSOR_LOWER		9
	#define	UNTIL_TIME					10
	int CheckTerminatingConditions(int TerminatingCondition,unsigned long TerminatingValue);	

	// Thermal Array defines & Prototypes: 
	unsigned char ambient;
	unsigned char PixelTemperature[8];
	int Readthermalarray(unsigned char);
	int GetHotPixel(unsigned char *PixelNumber,unsigned char *DeltaTemp);
	void GoToFlame(void);
	void FlameSensorTest(void);

	// USART Prototypes:
	void USART_Init(unsigned char);
	char isCharAvailable(void);
	char receiveChar(void);
	void sendChar(char ) ;
	void sendNumber(long);
	void sendString(char *);

	// I2C Defines and Prototypes:
	#define	SCL			PE4
	#define	SDA			PE5
	#define	SCL_Low()	SetBit(DDRE, SCL); SetBit(DDRE, SCL)		// make it an output
	#define	SCL_High()	ClearBit(DDRE, SCL); ClearBit(DDRE, SCL)	// make it an input
	#define	SDA_Low()	SetBit(DDRE, SDA)							// make it an output
	#define SDA_High()	ClearBit(DDRE, SDA)							// make it an input
	#define	I2C_TIMEOUT	1000

	void I2C_Init(void);
	void I2C_Start(void);
	void I2C_Stop(void);
	int I2C_Transmit(unsigned char);
//	int I2C_Receive(unsigned char *);
	int I2C_Receive(unsigned char *RxData, unsigned int last);

	// I2C Compass defines & prototypes:
	unsigned char Heading;
	int ReadCompass(void);
	
	int PCF8574_write(unsigned char pins);
	int DS1803_write(unsigned char pot, unsigned char value) ; 
	int gpio_write(char reg, char data);
	unsigned char gpio_read1(char reg);
	unsigned int gpio_read2(char reg);  
  


	// Timer/Counter1 Defines & Prototypes for driving RC Servo  
	#define servo_up()			OCR1A = 2000;			// 1.0ms 45 deg ccw from center
	#define servo_center()		OCR1A = 3000;			// 1.5ms Center Servo
	#define servo_down()		OCR1A = 4000;			// 2.0ms 45 deg cw from center
	void init_servo(void);

	// Defines for EEPROM
	#define EEPROMWriteByte(addr, byte) \
		eeprom_write_byte((uint8_t *)(addr), (byte))
	#define EEPROMReadByte(addr) \
		eeprom_read_byte((uint8_t *)(addr))
	
#endif
